﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;

namespace TfsComLib.TfsBranchLib2
{
    /// <summary>
    ///     Атрибуты ветки для поддержки пользовательского ввода
    /// </summary>
    public class BranchData : INotifyPropertyChanged, IComparable
    {
        /// <summary>
        ///     Для выбора подмножества
        /// </summary>
        private bool isSelected;

        /// <summary>
        ///     Имя ветки, должно совпадать с именем папки
        /// </summary>
        private string name;

        /// <summary>
        ///     Полный путь ветки в TFS. Пример: $/TDCode/trunk
        /// </summary>
        private string serverPath;

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                ChangeProperty("Name");
            }
        }

        public string ServerPath
        {
            get { return serverPath; }
            set
            {
                serverPath = value;
                ChangeProperty("ServerPath");
            }
        }

        /// <summary>
        ///     Описание ветки
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        ///     Для выбора подмножества
        ///     Notified for selection interaction
        /// </summary>
        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                isSelected = value;
                ChangeProperty("IsSelected");
            }
        }

        #region IComparable Members

        public int CompareTo(object obj)
        {
            string b;
            if (obj is BranchData)
            {
                b = (obj as BranchData).ServerPath;
            }
            else
            {
                b = null;
            }
            return string.Compare(ServerPath, b);
        }

        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        public override string ToString()
        {
            return string.Format("Name:{0}, Description:{1}, ServerPath:{2} ", Name, Description, ServerPath);
        }

        private void ChangeProperty(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        ///     Retrun comparer by ServerPath
        /// </summary>
        /// <returns></returns>
        public static IEqualityComparer<BranchData> GetBranchDataComparer()
        {
            return new BranchDataTestComparer();
        }

        public override bool Equals(object obj)
        {
            var a = this;
            var b = obj as BranchData;
            if (b == null) return false;
            if (a.ServerPath == b.ServerPath) return true;
            return false;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        ///     Comparer by ServerPath
        /// </summary>
        public class BranchDataTestComparer : IComparer<BranchData>, IEqualityComparer<BranchData>
        {
            #region IComparer<BranchDataTest> Members

            public int Compare(BranchData x, BranchData y)
            {
                return string.Compare(x.ServerPath, y.ServerPath);
            }

            #endregion

            #region IEqualityComparer<BranchDataTest> Members

            public bool Equals(BranchData x, BranchData y)
            {
                if (x.ServerPath == y.ServerPath) return true;
                return false;
            }

            public int GetHashCode(BranchData obj)
            {
                Trace.WriteLine(string.Format("{0} : {1}", obj.ServerPath, obj.ServerPath.GetHashCode()));
                return obj.ServerPath.GetHashCode();
            }

            #endregion
        }
    }
}